package com.dxl.lanju.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dxl.common.annotation.RequirePermission;
import com.dxl.common.enums.State;
import com.dxl.common.model.Page;
import com.dxl.common.model.RespData;
import com.dxl.common.utils.Utils;
import com.dxl.lanju.service.StockService;
import com.dxl.web.common.controller.BaseController;
import com.lanju.bean.StockBean;
import com.lanju.entity.Product;
import com.lanju.entity.ProductSku;
import com.lanju.entity.Shop;
import com.lanju.entity.Stock;

/**
 * 库存信息控制器
 * @author 邓小林
 */
@Controller
@RequestMapping("/stock")
@RequirePermission(value={"STOCK-MODULE"}, name="库存模块", desc="包括库存增、删、改、查")
public class StockController extends BaseController {
	@Autowired
	private StockService stockService;
	
	@RequestMapping("")
	@RequirePermission(value={"STOCK-VIEW"}, name="库存查看", desc="查看库存信息")
	public ModelAndView stock() {
		ModelAndView mv = new ModelAndView("stock/list");
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	@RequirePermission(value={"STOCK-VIEW"}, name="库存查看", desc="查看库存信息")
	public Page<StockBean> list(String areaId, String userId, String prodId, Integer state, Integer page, Integer rows) {
		Page<StockBean> p = stockService.list(areaId, userId, prodId, state, page, rows);
		return p;
	}
	
	@RequestMapping("/edit")
	@RequirePermission(value={"STOCK-VIEW"}, name="库存查看", desc="查看库存信息")
	public ModelAndView edit(String id, String areaId) {
		ModelAndView mv = new ModelAndView("stock/edit");
		if (Utils.isNotBlank(id)) {
			Stock stock = this.baseService.get(Stock.class, id);
			mv.addObject("entity", stock);
			if (Utils.isNotNull(stock)) {
				Product product = this.baseService.get(Product.class, stock.getProdId());
				mv.addObject("product", product);
				ProductSku productSku = this.baseService.get(ProductSku.class, stock.getProdSkuId());
				mv.addObject("productSku", productSku);
			}
		} else {
			if (Utils.isNotBlank(areaId)) {
				Stock stock = new Stock();
				stock.setAreaId(areaId);
				Map<String, Object> params = new HashMap<String, Object>();
				params.put("areaId", areaId);
				params.put("state", State.ACTIVE.getState());
				Page<Shop> page = this.baseService.list(Shop.class, params, 1, 1);
				if (page.getTotal() > 0) {
					stock.setUserId(page.getRows().get(0).getUserId());
				}
				mv.addObject("entity", stock);
			}
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	@RequirePermission(value={"STOCK-MODIFY"}, name="库存修改", desc="修改库存信息")
	public RespData save(Stock stock) {
		return stockService.save(stock);
	}
	
	@ResponseBody
	@RequestMapping("/del")
	@RequirePermission(value={"STOCK-DEL"}, name="库存删除", desc="删除库存信息")
	public RespData del(String[] ids) {
		return super.del(Stock.class, ids);
	}
	
	@ResponseBody
	@RequestMapping("/state")
	@RequirePermission(value={"STOCK-STATE"}, name="库存状态修改", desc="修改库存状态")
	public RespData state(String[] ids, Integer state) {
		return super.state(Stock.class, ids, state);
	}
}
